<style lang="stylus" rel="stylesheet/stylus">
  .article
    .title
      color #324057
      font-weight bold
</style>

<template>
  <div class="article">
    <el-breadcrumb>
      <el-breadcrumb-item :to="'/'">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main">
      <div class="header">
        <el-button type="primary" size="small" icon="plus" @click="articleAdd">新增</el-button>
      </div>
      <div class="table">
        <el-table :data="articles">
          <el-table-column label="标题">
            <template scope="scope">
              <router-link class="title" :to="'/layout/article/post/'+scope.row.id" v-text="scope.row.title"></router-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="类型"
            :formatter="typeFormatter">
          </el-table-column>
          <el-table-column
            prop="categoryNames"
            label="分类">
          </el-table-column>
          <el-table-column
            prop="tagNames"
            label="标签">
          </el-table-column>
          <el-table-column label="操作">
            <template scope="scope">
              <el-button
                size="small"
                type="text"
                @click="deleteArticle(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination
          @current-change="currentChange"
          :current-page="pageNumber"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import util from '../../assets/js/util'
  export default {
    data() {
      return {
        pageNumber:1,
        pageSize:15,
        total: 0,
        articles:[]
      }
    },
    methods: {
        init(){
          util.api.post('/api/admin/articles', {pageNumber:this.pageNumber,pageSize:this.pageSize})
            .then(data => {
              if (data.isOk) {
                this.total = data.total
                this.articles = data.articles
              } else {
                this.$message.error(data.errmsg)
              }
            })
        },
      currentChange(val){
        this.pageNumber = val;
        this.init()
      },
      deleteArticle(id){
        util.api.post('/api/admin/deleteArticle', {articleId:id})
          .then(data => {
            if (data.isOk) {
              this.$message.success('删除成功')
              this.init()
            }else{
              this.$message.error(data.errmsg)
            }
          })
      },
      typeFormatter(row){
        if (row.type === 1)
          return '正式'
        else if (row.type === 2)
          return '草稿'
      },
      articleAdd(){
          this.$router.push('/layout/article/post')
      }
    },
    created(){
      this.init()
    }
  }
</script>
